/*
 * @author : TF-BJ-C064
 * @creation : 2014-7-29 下午4:18:57
 * @description : 
 *
 */

package com.tool.tmap;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import com.action.system.qrservlet;
import com.base.BaseAction;
import com.util.GUtil;

public class TMapAction extends BaseAction{
	
	private Logger log = Logger.getLogger(qrservlet.class);
	
	private Double longitude;
	private Double latitude;
	
	SimpleDateFormat fileNameTimeFormat= new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss-SSS");

	@Override
	public String handle() throws Exception {
		
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");

		//reflect 调用Action中的方法
		Method function = null;
		try{
			function = this.getClass().getDeclaredMethod(method);
		}catch(Exception e){
			function = null;
		}
		if(function!=null){
			long t1 = System.currentTimeMillis();
			function.invoke(this);
			long t2 = System.currentTimeMillis();
			log.error("ip="+ip+" "+this.getClass()+"."+method+"(). takes time "+(t2-t1)+" ms");
		}else{
			log.error("ip="+ip+" "+this.getClass()+".handle() call function["+function+"] is not exist.");
		}
		return GUtil.NONE;
	}
	
	/**
	 * 转换百度地图坐标2 GPS坐标
	 * <p>Title: BMap2GPS</p>
	 * <p>Description: </p>
	 * @return
	 * @throws IOException
	 */
	public String BMap2GPS() throws IOException{
		GPSPoint point = TMap.convert.BMap2GPS(longitude, latitude);
		PrintWriter out = response.getWriter();
		JSONObject json = new JSONObject();
		json.put("error", 0);
		json.put("data", point.toString());
		out.write(json.toString());//json
		out.close();
		return GUtil.NONE;
	}
	
	/**
	 * 转换GPS坐标2百度地图坐标
	 * <p>Title: GPS2BMap</p>
	 * <p>Description: </p>
	 * @return
	 * 	 	转换后的返回数据，可参考百度地图API GPS坐标转换
	 * 		e.g:
	 *	 		{"status":0,"result":[{"x":114.23075574258,"y":29.579081083867},{"x":114.23075168254,"y":29.579086183217}]}
	 * @throws IOException
	 */
	public String GPS2BMap() throws IOException{
		String html = TMap.convert.GPS2BMapHttp(longitude, latitude, null);
		PrintWriter out = response.getWriter();
		out.write(html);//json
		out.close();
		return GUtil.NONE;
	}

	@Deprecated
	@Override
	public String query() throws Exception {
		return null;
	}
	@Deprecated
	@Override
	public String add() throws Exception {
		return null;
	}
	@Deprecated
	@Override
	public String update() throws Exception {
		return null;
	}
	@Deprecated
	@Override
	public String delete() throws Exception {
		return null;
	}
	@Deprecated
	@Override
	public String view() throws Exception {
		return null;
	}
	@Deprecated
	@Override
	public boolean validate(Object obj) throws Exception {
		return false;
	}

	public Double getLongitude() {
		return longitude;
	}

	public void setLongitude(Double longitude) {
		this.longitude = longitude;
	}

	public Double getLatitude() {
		return latitude;
	}

	public void setLatitude(Double latitude) {
		this.latitude = latitude;
	}

}
